/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.data;

import org.rails.extjs.AbsBase;
import org.rails.extjs.javascript.Function;

public class Field extends AbsBase {
	
	public static final String TYPE_AUTO = "auto";
	public static final String TYPE_STRING = "string";
	public static final String TYPE_INT = "int";
	public static final String TYPE_FLOAT = "float";
	public static final String TYPE_BOOLEAN = "boolean";
	public static final String TYPE_DATE = "date";
	
	public static final String SORT_ASC = "ASC";
	public static final String SORT_DESC = "DESC";

	public Field(int mapping,String name) {
		super();
		setMapping(mapping);
		setName(name);
	}
	
	public Field(String mapping,String name) {
		super();
		setMapping(mapping);
		setName(name);
	}
	
	public void setAllowBlank(Boolean allowBlank){
		addOption("allowBlank",allowBlank);
	}
	
	public Boolean isAllowBlank(){
		return getBoolean("allowBlank",false);
	}
	
	public void setConvert(Function convert){
		addOption("convert",convert);
	}
	
	public Function getConvert(){
		return (Function)getOption("convert");
	}
	
	public void setDateFormat(String dateFormat){
		addOption("dateFormat", dateFormat);
	}
	
	public String getDateFormat(){
		return getString("dateFormat","");
	}
	
	public void setDefaultValue(Object defaultValue){
		addOption("defaultValue",defaultValue);
	}
	
	public Object getDefaultValue(){
		return getString("defaultValue","");
	}
	
	protected void setMapping(Object mapping){
		addOption("mapping", mapping);
	}
	
	protected void setName(String name){
		addOption("name",name);
	}
	
	public void setSortDir(String sortDir){
		addOption("sortDir", sortDir);
	}
	
	public String getSortDir(){
		return getString("sortDir", SORT_ASC);
	}
	
	public void setSortType(Function sortType){
		addOption("sortType", sortType);
	}
	
	public Function getSortType(){
		return (Function)getOption("sortType");
	}
	
	public void setType(String type){
		addOption("type",type);
	}
	
	public String getType(){
		return getString("type",TYPE_AUTO);
	}

	@Override
	public String getScript() {
		script = new StringBuffer("");
		script.append("new Ext.data.Field(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}

}
